﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin;

namespace MiniWeb.Web.Controllers.Web
{
    public class UserBaseController : System.Web.Mvc.Controller
    {

        private readonly AccountsInfoTask _accountsInfoTask = new AccountsInfoTask();

        protected ActionResult AlertMsg(string msg, string returnUrl)
        {
            var script = string.Format("<script>alert('{0}');this.location.href='{1}';</script>", msg, returnUrl);
            Response.Write(script);
            Response.End();
            return new EmptyResult();
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                filterContext.HttpContext.Response.Write("<script>window.top.location.href='/user/login';</script>");
                filterContext.HttpContext.Response.End();
                return;
            }

          
        }
        public AccountsInfo GetCurrentWeb()
        {
            if (WebFormsAuthService.IsSignedIn())
            {
                return _accountsInfoTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());

            }
            return null;
        }

        public string LogOnNickName
        {
            get
            {
                return WebFormsAuthService.IsSignedIn() ? WebFormsAuthService.GetCurrentNickName() : string.Empty;
            }
        }
       
        public int? LoginAccountID
        {
            get
            {
                if(WebFormsAuthService.IsSignedIn())
                {
                    return GetCurrentWeb().ID;
                }
                else
                {
                    return null;
                }
            }
        }
        

    }
}
